package com.carrental.finance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.finance.dto.InvoiceDTO;
import com.carrental.finance.entity.Invoice;
import com.carrental.finance.service.InvoiceService;
import com.carrental.finance.vo.InvoiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 发票控制器
 */
@Api(tags = "发票管理")
@RestController
@RequestMapping("/invoices")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @ApiOperation("创建发票")
    @PostMapping
    public Result<Long> createInvoice(@RequestBody @Validated InvoiceDTO invoiceDTO) {
        Long id = invoiceService.createInvoice(invoiceDTO);
        return Result.success(id);
    }

    @ApiOperation("根据ID获取发票详情")
    @GetMapping("/{id}")
    public Result<InvoiceVO> getInvoiceById(@PathVariable Long id) {
        InvoiceVO invoiceVO = invoiceService.getInvoiceById(id);
        return Result.success(invoiceVO);
    }

    @ApiOperation("根据发票号获取发票详情")
    @GetMapping("/no/{invoiceNo}")
    public Result<InvoiceVO> getInvoiceByInvoiceNo(@PathVariable String invoiceNo) {
        InvoiceVO invoiceVO = invoiceService.getInvoiceByInvoiceNo(invoiceNo);
        return Result.success(invoiceVO);
    }

    @ApiOperation("根据订单ID获取发票详情")
    @GetMapping("/order/{orderId}")
    public Result<InvoiceVO> getInvoiceByOrderId(@PathVariable Long orderId) {
        InvoiceVO invoiceVO = invoiceService.getInvoiceByOrderId(orderId);
        return Result.success(invoiceVO);
    }

    @ApiOperation("根据用户ID分页查询发票")
    @GetMapping("/user/{userId}")
    public Result<Page<InvoiceVO>> pageUserInvoices(
            @PathVariable Long userId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size) {
        Page<Invoice> page = new Page<>(current, size);
        Page<InvoiceVO> result = invoiceService.pageUserInvoices(userId, page);
        return Result.success(result);
    }

    @ApiOperation("分页查询发票")
    @GetMapping("/page")
    public Result<Page<InvoiceVO>> pageInvoices(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("发票号") @RequestParam(required = false) String invoiceNo,
            @ApiParam("订单号") @RequestParam(required = false) String orderNo,
            @ApiParam("用户ID") @RequestParam(required = false) Long userId,
            @ApiParam("发票类型") @RequestParam(required = false) Integer invoiceType,
            @ApiParam("发票状态") @RequestParam(required = false) Integer status,
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime) {
        Page<Invoice> page = new Page<>(current, size);
        Page<InvoiceVO> result = invoiceService.pageInvoices(page, invoiceNo, orderNo, userId, invoiceType, status, startTime, endTime);
        return Result.success(result);
    }

    @ApiOperation("开具发票")
    @PostMapping("/issue/{id}")
    public Result<Boolean> issueInvoice(@PathVariable Long id) {
        boolean success = invoiceService.issueInvoice(id);
        return Result.success(success);
    }

    @ApiOperation("作废发票")
    @PostMapping("/invalidate/{id}")
    public Result<Boolean> invalidateInvoice(@PathVariable Long id) {
        boolean success = invoiceService.invalidateInvoice(id);
        return Result.success(success);
    }

    @ApiOperation("更新发票电子URL")
    @PostMapping("/url/{id}")
    public Result<Boolean> updateInvoiceUrl(
            @PathVariable Long id,
            @ApiParam("电子发票URL") @RequestParam String invoiceUrl) {
        boolean success = invoiceService.updateInvoiceUrl(id, invoiceUrl);
        return Result.success(success);
    }
} 